package com.luoxiang.socialandroid.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.THost;

import java.util.List;

public class FollowAdapter extends RecyclerView.Adapter<FollowAdapter.ViewHolder> {
    private List<THost> list;
    private String url = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/";

    public FollowAdapter(List<THost> list) {
        this.list = list;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int userid); //传递boolean类型数据给activity
    }

    public static interface OnAddClickListenerDs {
        // true add; false cancel
        void onItemClicksDs(int position, int userid);
    }

    public static interface OnAddClickListenerSx {

        void onItemClicksSx(int userid);
    }

    public static interface OnAddClickListenerShipin {

        void onItemClicksShipin(int userid);
    }

    OnAddClickListener onItemAddClick;

    OnAddClickListenerDs onItemAddClickDs;

    OnAddClickListenerSx onItemAddClickSx;

    OnAddClickListenerShipin onAddClickListenerShipin;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }

    public void setOnAddClickListenerDs(OnAddClickListenerDs onItemAddClickDs) {
        this.onItemAddClickDs = onItemAddClickDs;
    }

    public void setOnAddClickListenerSx(OnAddClickListenerSx onItemAddClickSx) {
        this.onItemAddClickSx = onItemAddClickSx;
    }

    public void setOnAddClickListenerShipin(FollowAdapter.OnAddClickListenerShipin onAddClickListenerShipin) {
        this.onAddClickListenerShipin = onAddClickListenerShipin;
    }

    @NonNull
    @Override
    public FollowAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_follow, parent, false);
        FollowAdapter.ViewHolder holder = new FollowAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull FollowAdapter.ViewHolder holder, int position) {
        THost hostBean = list.get(position);
        Glide.with(MyAppliaction.getContext())
                .load(url + hostBean.getIcon())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(holder.im1_follow);
        if (hostBean != null) {
            if (hostBean.getHost()==1){
                holder.im2_follow.setImageResource(R.mipmap.icon_ishostzhubo);
            }else {
                if (hostBean.getIdentity() == 10) {
                    holder.im2_follow.setImageResource(R.mipmap.icon_huangjin);
                } else if (hostBean.getIdentity() == 20) {
                    holder.im2_follow.setImageResource(R.mipmap.icon_bojin);
                } else if (hostBean.getIdentity() == 30) {
                    holder.im2_follow.setImageResource(R.mipmap.icon_zuanshi);
                } else {
                    holder.im2_follow.setVisibility(View.GONE);
                }
            }
        }

        if (hostBean.getCall() == 1) {
            holder.re_dashan.setVisibility(View.GONE);
            holder.im_sixin.setVisibility(View.VISIBLE);
        } else {
            holder.re_dashan.setVisibility(View.VISIBLE);
            holder.im_sixin.setVisibility(View.GONE);
        }
        if (holder.tx1_follow.getTextSize()>=5){
            holder.tx1_follow.setText(hostBean.getName()+"...");
        }else {
            holder.tx1_follow.setText(hostBean.getName());
        }
        holder.tx2_follow.setText(hostBean.getPersionSign());
        holder.im1_follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClick.onItemClicks(hostBean.getUserId());
            }
        });
        holder.re_dashan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClickDs.onItemClicksDs(position, hostBean.getUserId());
                if (hostBean.getCall() == 0) {
                    holder.re_dashan.setVisibility(View.GONE);
                    holder.im_sixin.setVisibility(View.VISIBLE);
                }
                notifyDataSetChanged();
            }
        });
        holder.im_sixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemAddClickSx.onItemClicksSx(hostBean.getUserId());
            }
        });
        holder.shipin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击视频
                onAddClickListenerShipin.onItemClicksShipin(hostBean.getUserId());
            }
        });
    }

    @Override
    public int getItemCount() {
        try {
            return list.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView im1_follow, im2_follow, im_sixin, shipin;
        private RelativeLayout re_dashan;
        private TextView tx1_follow, tx2_follow;


        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            im1_follow = itemView.findViewById(R.id.im1_follow);
            re_dashan = itemView.findViewById(R.id.re_dashan);
            im_sixin = itemView.findViewById(R.id.im_sixin);
            im2_follow = itemView.findViewById(R.id.im2_follow);
            tx1_follow = itemView.findViewById(R.id.tx1_follow);
            tx2_follow = itemView.findViewById(R.id.tx2_follow);
            shipin = itemView.findViewById(R.id.shipin);
        }
    }
}
